<template>
  <div class="checkout-address">
    <div class="text" v-if="ShowAddress">
      <div class="none" v-if="!ShowAddress">您需要先添加收货地址才可提交订单。</div>
      <ul>
        <li><span>收<i/>货<i/>人：</span>{{ShowAddress.receiver}}</li>
        <li><span>联系方式：</span>{{ShowAddress.contact}}</li>
        <li><span>收货地址：</span>{{ShowAddress.fullLocation}}</li>
      </ul>
      <a href="javascript:;" @click="openAdd(ShowAddress)" v-if="ShowAddress">修改地址</a>
    </div>
    <div class="action" v-if="ShowAddress">
      <XtxButton class="btn" @click="openDialog" >切换地址</XtxButton>
      <XtxButton class="btn" @click="openAdd({})">添加地址</XtxButton>
    </div>
  </div>
  <XtxDialog title="切换收货地址" v-model:visible="visibleDialog">
    <!-- 对话框内容 -->
    <div class="text item" v-for="item in list" :key="item.id" @click="selectedDialog = item" :class="{active:selectedDialog && selectedDialog.id === item.id}">
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
          <li><span>联系方式：</span>{{item.contact}}</li>
          <li><span>收货地址：</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
        </ul>
      </div>
    <!-- 确认取消 -->
    <template v-slot:footer>
        <XtxButton type="gray" style="margin-right:20px" @click="visibleDialog = false">取消</XtxButton>
        <XtxButton type="primary" @click="confirmDialog">确认</XtxButton>
    </template>
  </XtxDialog>
  <AddressEdit ref="openAI" @on-success="successHandler"/>
</template>
<script>
import AddressEdit from './Address-edit.vue'
import { reactive, ref, watch } from 'vue'
export default {
  name: 'CheckoutAddress',
  components:{ AddressEdit },
  props:{
    list:{
        type:Array
    }
  },
  // emits: ['change'],
  setup(props,{emit}){
    // 两种情况 -- 没有收货地址，就添加收获地址 -- 默认第一个是收获地址 
    // 生成订单的数据
    const ShowAddress = ref(null)
    if(props.list.length){
        // 为0默认地址
        ShowAddress.value = props.list.find(item => item.isDefault === 1)
    }else{
        // 收货地址为第一个
        ShowAddress.value = props.list[0]
    }
    
    // 对话框显示与隐藏
    const visibleDialog = ref(false)
    //组件初始化时将默认地址Id传给父组件
    emit('change', ShowAddress.value && ShowAddress.value.id)
    // 1.对话框显示
    const openDialog = () => {
      visibleDialog.value = true
      selectedDialog.value = null
    }
    // 2.确认对话框
    const selectedDialog = ref(null)
    const confirmDialog = () => {
      visibleDialog.value = false
      ShowAddress.value = selectedDialog.value
      emit('change',ShowAddress.value && ShowAddress.value.id)
    }
    //添加收获地址
    const openAI = ref(null)
    const openAdd = (address) => {
      // console.log(address.id,'address.id')
      openAI.value.open(address)
      
    }
    const successHandler = (formData) => {
      // 修改 -- 将新值给旧值
      const address = props.list.find(item => item.id === formData.id)
      if(address){
        for(let key in address){
          address[key] = formData[key]
        }
        
      }else{
      // 添加
      const json = JSON.stringify(formData)  // 需要克隆下，不然使用的是对象的引用
      // eslint-disable-next-line vue/no-mutating-props
      props.list.unshift(JSON.parse(json))
      }
    }
    return { ShowAddress, visibleDialog, openDialog, confirmDialog, selectedDialog, successHandler, openAdd, openAI }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>